/** 
 * 日期比较大小 
 * compareDateString大于dateString，返回1； 
 * 等于返回0； 
 * compareDateString小于dateString，返回-1 
 * @param {日期字符串} dateString
 * @param {比较的日期} compareDateString  
 */
export function dateCompare(dateString, compareDateString) {
  const dateTime = dateParse(dateString).getTime();
  const compareDateTime = dateParse(compareDateString).getTime();
  if (compareDateTime > dateTime) {
    return true;
  } else if (compareDateTime === dateTime) {
    return false;
  } else {
    return false;
  }
};
/**
 * 将日期字符串转换为Date
 * @param {日期字符串} dateString 
 */
export function dateParse(dateString) {
  const SEPARATOR_BAR = "-";
  const SEPARATOR_SLASH = "/";
  const SEPARATOR_DOT = ".";
  let dateArray;
  if (dateString.indexOf(SEPARATOR_BAR) > -1) {
    dateArray = dateString.split(SEPARATOR_BAR);
  } else if (dateString.indexOf(SEPARATOR_SLASH) > -1) {
    dateArray = dateString.split(SEPARATOR_SLASH);
  } else {
    dateArray = dateString.split(SEPARATOR_DOT);
  }
  return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]);
};

// 获取当前时间戳
export function getCurrentTimestamp() {
  return new Date().getTime();
}

// 时间戳 转换时间
export function format(shijianchuo) {
  //shijianchuo是整数，否则要parseInt转换
  const time = new Date(shijianchuo);
  const y = time.getFullYear();
  const m = time.getMonth() + 1;
  const d = time.getDate();
  const h = time.getHours();
  const mm = time.getMinutes();
  const s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

function add0(m) { return m < 10 ? '0' + m : m }


export function formatNoZeroDate(datestring ,splitStr='-'){
  let dataArr = datestring.split(splitStr);
  if(dataArr.length<=1){
    return datestring
  }
  dataArr = dataArr.map( i => add0(Number(i)))
  return dataArr.join(splitStr)
  
}